package com.sc.util;

import com.sc.pojo.Exam;

import com.sc.pojo.Title;
import com.sc.pojo.User;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.ArrayList;
import java.util.List;


public class ExcelImportAndDownload {
    public static void creatResponseWithFileName(HttpServletResponse response, String fileName){
        try {
            //去除首部空行
            response.reset();
            //response参数设置
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("UTF-8"), "iso8859-1") + ".xls");
            response.setHeader("Cache-Control", "no-cache");
            response.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
    public static void exportTrackInfoExcel(List<Title> ExamdDeatailList, HttpServletResponse response, HttpSession session) {
        //利用输出流，写入excel文件
        OutputStream outputStream = null;
        BufferedOutputStream bos = null;
        try {
            User user=(User) session.getAttribute("theUser");
            Exam exam=(Exam) session.getAttribute("loadexam");
            outputStream = response.getOutputStream();
            bos = new BufferedOutputStream(outputStream);
            //HSSFWorkbook，poi工具类提供的操作excel表的方法
            HSSFWorkbook wb = new HSSFWorkbook();
            //设置当前子表的名称（一张表里可以有很多张子表）
            HSSFSheet sheet = wb.createSheet("circleTag");
            //设置好<th>
            String[] titles = new String[]{"科目id","题目","答案","答案解析","选项A","选项B",
                    "选项C","选项D","难度"};
            int rowNum = 0;   //全局表格行数
            HSSFRow titleRow = sheet.createRow(rowNum++);  //操作表格中的某一行
            for (int i = 0; i < titles.length; i++) {
                HSSFCell cell = titleRow.createCell(i);    //操作表格中的某一行的某一列
                cell.setCellValue(titles[i]);   		   //设置列的值
                sheet.setColumnWidth(i, 20 * 256);
            }
            //<th>设置好后，开始向表格中插入数据
            //遍历每一行每一列，将实体类的每一个属性，填入对应列。一行一个实体类。
            if (ExamdDeatailList != null && ExamdDeatailList.size() > 0) {
                for (int i = 0; i < ExamdDeatailList.size(); i++) {
                    int cellNum = 0;    //列值
                    Title detail = ExamdDeatailList.get(i);  //具体的实体
                    try{
                        HSSFRow row = sheet.createRow(rowNum++);  //创建行操作者
                        HSSFCell cell_1 = row.createCell(cellNum++);  //创建该行的第一列操作者
                        cell_1.setCellValue(detail.getSid()); //第一列设置用户名属性

                        HSSFCell cell_2 = row.createCell(cellNum++);//创建该行的第二列操作者
                        cell_2.setCellValue(detail.getTitle());//第二列设置用户id属性

                        HSSFCell cell_3 = row.createCell(cellNum++);
                        cell_3.setCellValue(detail.getAnswer());

                        HSSFCell cell_4 = row.createCell(cellNum++);
                        cell_4.setCellValue(detail.getParse());

                        HSSFCell cell_5 = row.createCell(cellNum++);
                        cell_5.setCellValue(detail.getChoice1());

                        HSSFCell cell_6 = row.createCell(cellNum++);
                        cell_6.setCellValue(detail.getChoice2());

                        HSSFCell cell_7 = row.createCell(cellNum++);
                        cell_7.setCellValue(detail.getChoice3());

                        HSSFCell cell_8 = row.createCell(cellNum++);
                        cell_8.setCellValue(detail.getChoice4());

                        HSSFCell cell_9 = row.createCell(cellNum++);
                        cell_9.setCellValue(detail.getDifficulty());

                    }catch (Exception e){
                    }
                }
            }
            wb.write(bos);
            bos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bos != null) {
                    bos.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
